package com.example.minzhengtong

import android.content.Context
import android.content.Intent
import android.media.AudioRecord.MetricsConstants.SOURCE
import android.os.Bundle
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestListener
import com.example.minzhengtong.base.BaseA
import com.example.minzhengtong.databinding.ActivitySplashBinding
import com.example.minzhengtong.utils.LogUtil
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import pl.droidsonroids.gif.GifDrawable
import pl.droidsonroids.gif.GifImageView
import java.util.concurrent.TimeUnit

/**
 * created by WY on 2021/6/2.
 */
class SplashA : BaseA() {

    companion object{
        fun enterThis(context: Context){
            val intent = Intent(context,SplashA::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var binding : ActivitySplashBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        overridePendingTransition(0,0)
        binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        Glide.with(baseContext).asGif().load(R.mipmap.splash)
//            .diskCacheStrategy(DiskCacheStrategy.RESOURCE).into(binding.image)

        //module里的GIFVIEW
//        binding.image.gifResource = R.mipmap.splash
//        binding.image.play()

        //depencendices里的GIF
        (binding.image.drawable as GifDrawable).start()
        (binding.image.drawable as GifDrawable).loopCount = 1

        setToolBarVisiabale(false)
        initJump()
    }

    private var stop : Boolean = false
    private var disposable : Disposable? = null
    private val waitTime = 2500

    private fun initJump() {
        Observable.interval(0,1, TimeUnit.MILLISECONDS)
            .takeWhile { !stop }
            .doOnSubscribe { disposable = it }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (it >= waitTime){
                    MainActivity.enterThis(baseContext)
                    stop = true
                    if (disposable != null && disposable?.isDisposed!!) {
                        disposable?.dispose()
                        disposable = null
                    }
                    finish()
                }
            }
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0,0)
    }

    override fun onDestroy() {
        if (disposable != null && disposable?.isDisposed!!) {
            disposable?.dispose()
            disposable = null
        }
        stop = true
        super.onDestroy()
    }

}